﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class pro_mis_level_editlevel2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Request.QueryString[1] != null)
            {
                int levelmissionid = int.Parse(Request.QueryString[1].ToString());
                DataTable dt = MasApp.BLL.Search.Level2(levelmissionid);
                MasApp.Model.applevel appl = new MasApp.Model.applevel();
                appl.Levelmissionid = levelmissionid;
                foreach (GridViewRow gr in GridView1.Rows)
                {
                    DropDownList ddl = (DropDownList)gr.FindControl("ddl_level");
                    if (ddl != null)
                    {
                        ddl.DataSource = dt;
                        ddl.DataTextField = "judgement";
                        ddl.DataValueField = "levelid";
                        ddl.DataBind();
                        Label lb = (Label)gr.FindControl("Label1");
                        appl.Indicatorid = int.Parse(lb.Text);
                        appl.Levelid = MasApp.BLL.Search.LevelIDinApplevel(appl);
                        Label lb_cur = (Label)gr.FindControl("lb_cur");
                        if (appl.Levelid > 0)
                        {
                            lb_cur.Text = ddl.Items.FindByValue(appl.Levelid.ToString()).Text;
                        }
                    }
                }
            }
            else
            {
                MasApp.BLL.ResponseHelper.alert("非法链接", "editlevel.aspx");
            }
        }
        Response.Write("<a href='editlevel.aspx?levelmissionid=" 
            + Request.QueryString[1].ToString() + "'>返回准则层</a><hr />");
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        MasApp.Model.applevel appl = new MasApp.Model.applevel();
        appl.Levelmissionid = int.Parse(Request.QueryString[1].ToString());

            bool b=true;
        foreach (GridViewRow gr in GridView1.Rows)
        {
            Label lb = (Label)gr.FindControl("Label1");
            appl.Indicatorid = int.Parse(lb.Text);
            DropDownList ddl = (DropDownList)gr.FindControl("ddl_level");
            appl.Levelid = int.Parse(ddl.SelectedValue);
            appl.Applevelid = MasApp.BLL.Search.HaveAppLevelID(appl);
            //Response.Write(appl.Levelid.ToString() + "/" + appl.Levelmissionid.ToString() + "/" + appl.Indicatorid.ToString() + "/" + appl.Applevelid.ToString()+"<br>");
            if (appl.Applevelid > 0)
            {
                if(!(MasApp.BLL.Update.UpdateAppLevelValue(appl)>0))
                {b=false;break;}
            }
            else
            {
                if(!(MasApp.BLL.reg.InsertAppLevel(appl)>0))
                {b=false;break;}
            }
        }
        string s;
        if (b) { s = "操作完成！"; }
        else
        {
            s = "操作失败";
        }
        MasApp.BLL.ResponseHelper.alert(s, "editlevel2.aspx?ruleid="
            + Request.QueryString[0].ToString() + "&levelimssionid=" + Request.QueryString[1].ToString());
    }
}
